/* $RCSFile: EnhancedLinksDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/EnhancedLinks/EnhancedLinksDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:53  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.Settings;

namespace AddonNice.Settings.PageModules.EnhancedLink
{
    public class EnhancedLinkDB : DBDataStore
    {
		/// <summary>
        /// The GetEnhancedLinks method returns a SqlDataReader containing all of the
        /// links for a specific portal module from the database.
		/// </summary>
        public SqlDataReader GetEnhancedLinks(int moduleID) 
        {
       
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetEnhancedLinks", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

            AddModuleIDParameters(myCommand,moduleID);

			SqlDataReader result			=	null;
			try
			{
				myConnection.Open();
				result						=	myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksdDB GetEnhancedLinks moduleID: {0}, ex : {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
            return result;
        }

        public SqlDataReader GetEnhancedLinksGlobalized(int moduleID)
        {

            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetEnhancedLinksGlobalized", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

            AddModuleIDParameters(myCommand,moduleID);

			AddGlobalizeParameters(myCommand);

			SqlDataReader result			=	null;
			try
			{
				myConnection.Open();
				result						=	myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksdDB GetEnhancedLinksGlobalized moduleID: {0}, ex : {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
        }

		/// <summary>
		/// The GetSingleEnhancedLink method returns a SqlDataReader containing details
		/// about a specific link from the EnhancedLinks database table.
		/// </summary>
        public SqlDataReader GetSingleEnhancedLink(int itemID) 
        {
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetSingleEnhancedLink", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			SqlDataReader result			=	null;
			try
			{
				myConnection.Open();
				result						=	myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksdDB GetSingleEnhancedLink itemID: {0}, ex : {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
        }

        public SqlDataReader GetSingleEnhancedLinkGlobalized(int itemID)
        {
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_GetSingleEnhancedLinkGlobalized", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			AddGlobalizeParameters(myCommand);

			SqlDataReader result			=	null;
			try
			{
				myConnection.Open();
				result						=	myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksdDB GetSingleEnhancedLinkGlobalized itemID: {0}, ex : {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
        }

		/// <summary>
		/// The DeleteEnhancedLink method deletes a specified link from
		/// the EnhancedLinks database table.
		/// </summary>
        public void DeleteEnhancedLink(int itemID) 
        {
            SqlCommand myCommand            =   new SqlCommand("dbo.adn_DeleteEnhancedLink", myConnection);
            myCommand.CommandType           =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksDB DeleteEnhancedLink ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
        }

		/// <summary>
		/// The AddEnhancedLink method adds a new link within the
		/// EnhancedLinks database table, and returns ItemID value as a result.
		/// </summary>
        public int AddEnhancedLink(int moduleID, int itemID, String userName, String title, String url, 
        						   int viewOrder, String description, String imageUrl, int clicks, String target) 
        {
            if (userName.Length < 1) 
            {
                userName                        =   StStr.unknown;
            }

            SqlCommand myCommand                =   new SqlCommand("dbo.adn_AddEnhancedLink", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            SqlParameter parameterItemID		=	AddItemIDParameters(myCommand,itemID);
            parameterItemID.Direction           =   ParameterDirection.InputOutput;

            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterUserName      = new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
            parameterUserName.Value             = userName;
            myCommand.Parameters.Add(parameterUserName);

            SqlParameter parameterTitle         = new SqlParameter("@Title", SqlDbType.NVarChar, 100);
            parameterTitle.Value                = title;
            myCommand.Parameters.Add(parameterTitle);

            SqlParameter parameterDescription   = new SqlParameter("@Description", SqlDbType.NVarChar, 100);
            parameterDescription.Value          = description;
            myCommand.Parameters.Add(parameterDescription);

            SqlParameter parameterImageUrl      = new SqlParameter("@ImageUrl", SqlDbType.NVarChar, 250);
            parameterImageUrl.Value             = imageUrl;
            myCommand.Parameters.Add(parameterImageUrl);

            SqlParameter parameterClicks        = new SqlParameter("@Clicks", SqlDbType.Int, 4);
            parameterClicks.Value               = clicks;
            myCommand.Parameters.Add(parameterClicks);

			SqlParameter parameterTarget        = new SqlParameter("@Target", SqlDbType.NVarChar, 10);
			parameterTarget.Value               = target;
			myCommand.Parameters.Add(parameterTarget);

            SqlParameter parameterUrl           = new SqlParameter("@Url", SqlDbType.NVarChar, 800);
            parameterUrl.Value                  = url;
            myCommand.Parameters.Add(parameterUrl);

			SqlParameter parameterViewOrder     = new SqlParameter("@ViewOrder", SqlDbType.Int, 4);
			parameterViewOrder.Value            = viewOrder;
			myCommand.Parameters.Add(parameterViewOrder);
			
            int ret                             =   -1;
			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
                ret                             =   (int)parameterItemID.Value;
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksDB AddEnhancedLink ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
            return ret;
        }

		/// <summary>
		/// The UpdateEnhancedLink method updates a specified link within
		/// the EnhancedLinks database table.
		/// </summary>
        public void UpdateEnhancedLink(int moduleID, int itemID, String userName, String title, String url, 
        							   int viewOrder, String description, String imageUrl, int clicks, String target) 
        {
            if (userName.Length < 1) 
            {
                userName                        =   StStr.unknown;
            }

            SqlCommand myCommand                =   new SqlCommand("dbo.adn_UpdateEnhancedLink", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

            SqlParameter parameterUserName      =   new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
            parameterUserName.Value             =   userName;
            myCommand.Parameters.Add(parameterUserName);

            SqlParameter parameterTitle         =   new SqlParameter("@Title", SqlDbType.NVarChar, 100);
            parameterTitle.Value                =   title;
            myCommand.Parameters.Add(parameterTitle);

            SqlParameter parameterDescription   =   new SqlParameter("@Description", SqlDbType.NVarChar, 100);
            parameterDescription.Value          =   description;
            myCommand.Parameters.Add(parameterDescription);

            SqlParameter parameterImageUrl      =   new SqlParameter("@ImageUrl", SqlDbType.NVarChar, 250);
            parameterImageUrl.Value             =   imageUrl;
            myCommand.Parameters.Add(parameterImageUrl);

            SqlParameter parameterClicks        =   new SqlParameter("@Clicks", SqlDbType.Int, 4);
            parameterClicks.Value               =   clicks;
            myCommand.Parameters.Add(parameterClicks);

            SqlParameter parameterUrl           =   new SqlParameter("@Url", SqlDbType.NVarChar, 800);
            parameterUrl.Value                  =   url;
            myCommand.Parameters.Add(parameterUrl);

            SqlParameter parameterViewOrder     =   new SqlParameter("@ViewOrder", SqlDbType.Int, 4);
            parameterViewOrder.Value            =   viewOrder;
            myCommand.Parameters.Add(parameterViewOrder);

            SqlParameter parameterTarget        =   new SqlParameter("@Target", SqlDbType.NVarChar, 10);
            parameterTarget.Value               =   target;
            myCommand.Parameters.Add(parameterTarget);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksDB UpdateEnhancedLink ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
        }
        
        public void UpdateEnhancedLinkGlobalized(int moduleID, int itemID, String userName, String title, String url,
                                       int viewOrder, String description, String imageUrl, int clicks, String target)
        {
            if (userName.Length < 1)
            {
                userName                        =   StStr.unknown;
            }

            SqlCommand myCommand                =   new SqlCommand("dbo.adn_UpdateEnhancedLinkGlobalized", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);
            AddModuleIDParameters(myCommand,moduleID);
			AddGlobalizeParameters(myCommand);

            SqlParameter parameterUserName      =   new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
            parameterUserName.Value             =   userName;
            myCommand.Parameters.Add(parameterUserName);

            SqlParameter parameterTitle         =   new SqlParameter("@Title", SqlDbType.NVarChar, 100);
            parameterTitle.Value                =   title;
            myCommand.Parameters.Add(parameterTitle);

            SqlParameter parameterDescription   =   new SqlParameter("@Description", SqlDbType.NVarChar, 100);
            parameterDescription.Value          =   description;
            myCommand.Parameters.Add(parameterDescription);

            SqlParameter parameterImageUrl      =   new SqlParameter("@ImageUrl", SqlDbType.NVarChar, 250);
            parameterImageUrl.Value             =   imageUrl;
            myCommand.Parameters.Add(parameterImageUrl);

            SqlParameter parameterClicks        =   new SqlParameter("@Clicks", SqlDbType.Int, 4);
            parameterClicks.Value               =   clicks;
            myCommand.Parameters.Add(parameterClicks);

            SqlParameter parameterUrl           =   new SqlParameter("@Url", SqlDbType.NVarChar, 800);
            parameterUrl.Value                  =   url;
            myCommand.Parameters.Add(parameterUrl);

            SqlParameter parameterViewOrder     =   new SqlParameter("@ViewOrder", SqlDbType.Int, 4);
            parameterViewOrder.Value            =   viewOrder;
            myCommand.Parameters.Add(parameterViewOrder);

            SqlParameter parameterTarget        =   new SqlParameter("@Target", SqlDbType.NVarChar, 10);
            parameterTarget.Value               =   target;
            myCommand.Parameters.Add(parameterTarget);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]EnhancedLinksDB UpdateEnhancedLinkGlobalized ex: {0}",exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
        }
    }
}
